<?php
/*
   Copyright 2012 BiSe Trojanov

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
 require_once('config.php');
 set_time_limit(2*3600);

?><!doctype html><html>
<head>
 <?php include('./include/header.inc'); ?>
 <title><?php echo $LANGS['uploadfiles']['title'].' / '.$sad_domain; ?></title>
 <script type="text/javascript" src="<?php echo $sad_baseurl; ?>/common/bquery_ajax.js"></script>
 <script type="text/javascript" src="<?php echo $sad_baseurl; ?>/common/jquery.filedrop.js"></script>
 <link rel="stylesheet" href="<?php echo $sad_baseurl; ?>/common/ascet.css" type="text/css" media="screen">
 <script src="<?php echo $sad_baseurl; ?>/i/admin.js.php"></script>
<style>
div.uploadbox {
    -moz-border-radius: 3px 3px 3px 3px;
    border-radius: 3px 3px 3px 3px;
    border: 1px solid #BA7FFF;
    float: right;
    margin-left: 10%;
	padding: 67px 0;
    text-align: center;
    width: 500px;
}

td.status{
	width: 15px;
}

td.filename{
	width: 100px;
	max-width: 100px;
	overflow: hidden;
}

table.uploadtable{
    width: 100%;
	border: none;
	border-collapse: collapse;
}

table.uploadtable td{
    border: none;
}
</style>
</head>
<body>
<?php include('./include/toolbar.inc');?>

<div class="infobox"><?php echo $LANGS['uploadfiles']['infobox']; ?><table>
<?php echo '<tr><td title="post_max_size">'.$LANGS['index']['l_post_max'].'</td><td>'.drawsize(parsesize(ini_get('post_max_size')),'-.2').'</td></tr>';
 echo '<tr><td title="upload_max_filesize">'.$LANGS['index']['l_upload_max'].'</td><td>'.drawsize(parsesize(ini_get('upload_max_filesize')),'-.2').'</td></tr>'; ?>
</table></div>

<?php
 $t1   =explode('/',str_replace('\\','/',$_POST['path']));
 if (!file_exists("$sad_root/storage")){mkdir("$sad_root/storage");}
 $path ='';foreach ($t1 as $t2){
  if ($t2=='.' or $t2=='..' or $t2==''){continue;}
  $path.='/'.$t2;
  if (!file_exists("$sad_root/storage$path")){mkdir("$sad_root/storage$path");}
 }
 unset($t1,$t2);

 if (isset($_FILES['file']['tmp_name'])){
  if (((int)$_POST['overwrite'])>0){
   echo 'Overwrite all existing files<br />';
  }else{
   echo 'Skip existing files<br />';
  }
  echo "Path: /storage$path<br />";

  foreach ($_FILES['file']['tmp_name'] as $key => $tmp){
   if (strlen($tmp)<1){
    if (strlen($_FILES['file']['name'][$key])<1){continue;}
    echo '<span style="color: red"><strong>'.sad_safe_html($_FILES['file']['name'][$key]).'</strong> not uploaded';
    if ($_FILES['file']['error'][$key]==1 or $_FILES['file']['error'][$key]==2){echo ' (too large file, limit is '.ini_get('upload_max_filesize').' and '.ini_get('post_max_size').')';}
    echo '</span><br />';
    continue;
   }
   $new_name=preg_replace('|(.*/)?([^/]+)$|','$2',$_FILES['file']['name'][$key]);
   echo '<a href="'.$sad_baseurl.'/storage'.$path.'/'.$new_name.
        '" target="_blank">'.sad_safe_html($new_name).'</a> ';
   $u=true; if (file_exists($sad_root.'/storage'.$path.'/'.$new_name)){
    echo 'exists...';
    if ((int)$_POST['overwrite']>0){echo ' overwriten';}else{echo ' <strong>not uploaded</strong>';$u=false;}
   }
   if ($u){if (!copy($tmp,$sad_root.'/storage'.$path.'/'.$new_name)){echo '<span style="color: red">not copied</span>';}}
   echo ' (size: <strong>'.filesize($sad_root.'/storage'.$path.'/'.$new_name).'</strong> bytes)<br />';
  }
 }//if isset

?>
<!-- Ajax upload form -->
<div class="uploadbox sad-violet"><?php echo $LANGS['uploadfiles']['drop_here'];?><table class="uploadtable"></table></div>
<script>
var dropbox = $('div.uploadbox');
var hidedrop=navigator.userAgent.search('Firefox')==-1 &&
             navigator.userAgent.search('Chrome') ==-1;
if (hidedrop){dropbox.hide();}else{

 var active_upload=0;
 var set=dropbox.filedrop({
  paramname:   'pic',
  maxfiles:    100,
  maxfilesize: <?php echo floor(parsesize(ini_get('upload_max_filesize'))/1024/1024); ?>,
  url: '<?php echo $sad_baseurl; ?>/i/ajax.php?action=uploadfiles',
  data: {
   'dir':function(){return $('#path0')[0].value;},
   'overwrite':function(){return $('#overwrite')[0].checked ? 1 : 0;}
  },

  uploadStarted:function(i, file, len){
   var tr  =$("#fileupload"+i)[0];
   if (tr==undefined){
    var tr  =document.createElement('tr');
    tr.id    ="fileupload"+i;
    $('table.uploadtable')[0].appendChild(tr);
   }else{
    $(tr).html('');
    $(tr).show();
   }
   
   var td=document.createElement('td');
   tr.appendChild(td);$(td).addClass('status');$(td).html(file.size);
   var td=document.createElement('td');
   tr.appendChild(td);$(td).addClass('filename');$(td).html(file.name);
   
   var pb_d=document.createElement('td');
   tr.appendChild(pb_d);$(pb_d).css({
    'width':300,
	'height':14,
	'background-color':'white',
	'display':'inline-block',
	'padding':0,
	'border-radius':0,
	'-moz-border-radius':0
   });
   var pb  =document.createElement('div');
   pb_d.appendChild(pb);
   $(pb).addClass('progress-bar');
   $(pb).css({'width':'0%', 'height':14});
   pb.title='0%';
   
   active_upload++;
   //createImage(file);
  },
  progressUpdated:function(i, file, progress){
   $("#fileupload"+i+' div.progress-bar').css({'width':progress+'%'});
   $("#fileupload"+i+' div.progress-bar')[0].title=progress+'%';
  },
  uploadFinished:function(i,file,response){
   $("#fileupload"+i+' div.progress-bar').css({'width':'100%'});
   $("#fileupload"+i+' div.progress-bar')[0].title='100%';
   setTimeout(function(){$("#fileupload"+i).fadeOut(500);}, 5000);
   active_upload--;
   if (response.status=='403'){
    $("#fileupload"+i).addClass('sad-red');
	$("#fileupload"+i+' td.status').html('no access');
    return;
   }
   if (response.status=='skip'){
    $("#fileupload"+i).addClass('sad-orange');
	$("#fileupload"+i+' td.status').html('skip');
	return;
   }
   if (expl_set[0].base_path=="/"+response.dir){
    files[files.length]=response;
	refresh_all(0);
   }
  },
  error: function(err, file){},
  rename:function(old){
   var s=''; for (var i=0;i<old.length;i++){
    var c=old.charCodeAt(i);
	if (c>128 || c==32){s+='_';}else{s+=String.fromCharCode(c);}
   }
   return s;
  }
 });
}
</script>

<!-- POST upload form -->
<form method="post" enctype="multipart/form-data">
<table class="noborder">
 <tr><td style="text-align: right;"><label for="overwrite" title="<?php echo $LANGS['uploadfiles']['overwrite_title'];?>"><?php echo $LANGS['uploadfiles']['overwrite'];?></label></td><td><input type="checkbox" name="overwrite" value="1" id="overwrite"></td></tr>
 <tr><td style="text-align: right;"><label for="path" title="<?php echo $LANGS['uploadfiles']['folder_title'];?>"><?php echo $LANGS['uploadfiles']['folder'];?></label></td><td><input name="path" value="<?php
  if (strlen($_POST['path'])>0){echo sad_safe_html($_POST['path']);}else{echo '/';}
 ?>" id="path0"></td></tr>
 <tr><td colspan="2"><hr></td></tr>
 
 <tr><td style="text-align: right;"><label for="files"><?php echo $LANGS['uploadfiles']['files'];?></label></td><td><input type="file" multiple="multiple" name="file[]" id="files" /></td></tr>
 <tr><td style="text-align: center;" colspan="2"><input type="submit" class="nicebutton" value="<?php echo $LANGS['uploadfiles']['uploadbutton'];?>"></td></tr>
</table>

</form>

<!-- new code -->
<a href="javascript:" onclick="set_mode('a');" class="func">Access time</a>, 
<a href="javascript:" onclick="set_mode('m');" class="func">Change time</a>, 
<a href="javascript:" onclick="set_mode('c');" class="func">Inode time</a><br><br style="clear: both; ">
<div class="browser" id="browser0"></div>
<input id="new_folder_name"><br><input type="submit" value="Create folder" class="nicebutton" onclick="CreateFolder('storage', 1, 0);">
<script>
 var expl_set     =[
  {base_path:'/', parent_folder: 'storage', g_type: 1, need_check: false}
 ];
 viewfolder('<?php echo sad_safe_mysql($_POST['path']); ?>', 0);
</script>

</body></html>